<?php
/**
 * @Author:         zk
 * @DateTime:       18-4-3 下午3:35
 * @Description:    冲洗历史保养订单 同步到 order_history_maintain 历史保养订单统计表
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
set_time_limit(0);
//DB::connection()->disableQueryLog();      //关闭日志

$num = 365;     //冲刷数据天数
$res = flush_history_maintain($num);

/**
 * 清洗有效的历史保养订单 同步到 order_history_maintain 历史保养订单统计表
 * @param int $num  天数
 * @return type
 */
function flush_history_maintain($num){
    $order_status = [1117, 1217, 1417, 1018, 1420]; //订单状态
    $created_at = date('Y-m-d H:i:s', time());
    $note = '历史保养订单统计';

    for($i=$num; $i>0; $i--){
        $day_str = '- '.$i.' day';
        $start_time = date('Y-m-d', strtotime($day_str)).' 00:00:00';   //开始时间
        $end_time = date('Y-m-d', strtotime($day_str)).' 23:59:59';     //结束时间
        $list = DB::table('orders')
            ->leftJoin('order_product', 'orders.order_id', '=', 'order_product.order_id')
            ->where('orders.pay_time', '>', $start_time)
            ->where('orders.pay_time', '<', $end_time)
            ->whereIn('orders.status', $order_status)
            ->where('order_product.status', 1)
            ->orderBy('orders.pay_time', 'asc')
            ->select('orders.order_id', 'orders.user_id', 'orders.status', 'orders.name', 'orders.phone', 'orders.created_at',  
                    'orders.car_brand_id', 'orders.car_brand_name', 'orders.car_series_id', 'orders.car_series_name', 
                    'orders.car_model_id', 'orders.car_model_name', 'orders.shop_id', 'orders.user_car_id', 
                    'orders.car_no', 'orders.pay_time', 'order_product.product_type', 'order_product.product_id', 
                    'order_product.product_name')
            ->get();

        if(empty($list)){
            echo '......'.$start_time.'--'.$end_time.' 无基本保养订单 .......\n';
            continue;
        }
        $insert_arr = [];
        foreach ($list as $v) {
            if($v->product_id == 2){    //订单产品 中的product_id为2 基础保养
                $insert_arr[$v->order_id] = [
                    'order_id' => $v->order_id,
                    'user_id' => $v->user_id,
                    'status' => $v->status,
                    'name' => $v->name,
                    'phone' => $v->phone,
                    'user_car_id' => $v->user_car_id,
                    'car_no' => $v->car_no,
                    'created_time' => $v->created_at,
                    'pay_time' => $v->pay_time,
                    'car_brand_id' => $v->car_brand_id,
                    'car_brand_name' => $v->car_brand_name,
                    'car_series_id' => $v->car_series_id,
                    'car_series_name' => $v->car_series_name,
                    'car_model_id' => $v->car_model_id,
                    'car_model_name' => $v->car_model_name,
                    'shop_id' => $v->shop_id,
                    'product_type' => $v->product_type,
                    'product_id' => $v->product_id,
                    'product_name' => $v->product_name,
                    'created_at' => $created_at,
                    'note' => $note,
                ];
            } elseif ($v->product_type == 'activity') {     //order_product_acpackage"订单活动包的产品包"
                $ac_res = DB::table('order_product_acpackage')
                        ->where('order_id', $v->order_id)
                        ->where('package_id', 2)
                        ->select('package_id', 'package_name')
                        ->first();
                if($ac_res){
                    $insert_arr[$v->order_id] = [
                        'order_id' => $v->order_id,
                        'user_id' => $v->user_id,
                        'status' => $v->status,
                        'name' => $v->name,
                        'phone' => $v->phone,
                        'user_car_id' => $v->user_car_id,
                        'car_no' => $v->car_no,
                        'created_time' => $v->created_at,
                        'pay_time' => $v->pay_time,
                        'car_brand_id' => $v->car_brand_id,
                        'car_brand_name' => $v->car_brand_name,
                        'car_series_id' => $v->car_series_id,
                        'car_series_name' => $v->car_series_name,
                        'car_model_id' => $v->car_model_id,
                        'car_model_name' => $v->car_model_name,
                        'shop_id' => $v->shop_id,
                        'product_type' => $v->product_type,
                        'product_id' => $ac_res->package_id,        //order_product_acpackage 中的 产品包id
                        'product_name' => $ac_res->package_name,    //order_product_acpackage 中的 产品包名称
                        'created_at' => $created_at,
                        'note' => $note,
                    ];
                }
            } elseif ($v->product_type == 'enterprise') {   //order_product_espackage"订单企业包的产品包"
                $es_res = DB::table('order_product_espackage')
                        ->where('order_id', $v->order_id)
                        ->where('package_id', 2)
                        ->select('package_id', 'package_name')
                        ->first();
                if($es_res){
                    $insert_arr[$v->order_id] = [
                        'order_id' => $v->order_id,
                        'user_id' => $v->user_id,
                        'status' => $v->status,
                        'name' => $v->name,
                        'phone' => $v->phone,
                        'user_car_id' => $v->user_car_id,
                        'car_no' => $v->car_no,
                        'created_time' => $v->created_at,
                        'pay_time' => $v->pay_time,
                        'car_brand_id' => $v->car_brand_id,
                        'car_brand_name' => $v->car_brand_name,
                        'car_series_id' => $v->car_series_id,
                        'car_series_name' => $v->car_series_name,
                        'car_model_id' => $v->car_model_id,
                        'car_model_name' => $v->car_model_name,
                        'shop_id' => $v->shop_id,
                        'product_type' => $v->product_type,
                        'product_id' => $es_res->package_id,        //order_product_espackage 中的 产品包id
                        'product_name' => $es_res->package_name,    //order_product_espackage 中的 产品包名称
                        'created_at' => $created_at,
                        'note' => $note,
                    ];
                }
            }
        }
        if(!empty($insert_arr)){
            $res = DB::table('order_history_maintain')->insert($insert_arr);    //插入历史保养订单统计表
            if($res){
                print_r('......'.$start_time.'--'.$end_time.' 历史保养订单 统计成功  ok .......\n');
            }else{
                print_r('......'.$start_time.'--'.$end_time.' 历史保养订单 统计失败 error .......\n');
            }    
        }
        
        unset($insert_arr);
        unset($list);
    }
    
}





